﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Bpms.Data;
using Bpms.Entity;
using Bpms.Log;
using Bpms.Cache;

namespace Bpms.Bll
{
    public class Customer
    {
        #region Member
        /// <summary>
        /// 数据库改动事件委托
        /// </summary>
        public delegate void DBChangedEventHandler();

        /// <summary>
        /// 日志事件委托
        /// </summary>
        public delegate void LogEventHandler();

        /// <summary>
        /// 数据库改动事件
        /// </summary>
        public static event DBChangedEventHandler DBChanged;

        /// <summary>
        /// 写日志事件
        /// </summary>
        public static event LogEventHandler WriteLog;

        #region Private

        private static IDataProvider provider = DatabaseProvider.GetInstance();

        /// <summary>
        /// 数据改动事件处理函数
        /// </summary>
        static void OnDBChanged()
        {
            if (DBChanged != null)
            {
                DBChanged();
            }
        }

        /// <summary>
        /// 写日志处理函数
        /// </summary>
        static void OnWriteLog()
        {
            if (WriteLog != null)
            {
                WriteLog();
            }
        }

        #endregion

        #endregion

        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="jobTitle">客户实体对象</param>
        /// <returns>-1：存在相同的记录（客户名称相同），0：添加失败，1：添加成功</returns>
        public static int InsertCustomer(CustomerInfo customer)
        {   // 判断是否存在相同名称的记录
            if (provider.CustomerExists(customer, OperateType.Insert))
            {
                return -1;
            }

            if (provider.InsertCustomer(customer))
            {
                OnDBChanged();
                OnWriteLog();
                return 1;
            }

            return 0;
        }

        /// <summary>
        /// 修改客户
        /// </summary>
        /// <param name="jobTitle">客户实体对象</param>
        /// <returns>-1：存在相同的记录（客户名称相同），0：修改失败，1：修改成功</returns>
        public static int UpdateCustomer(CustomerInfo customer)
        {
            if (provider.CustomerExists(customer, OperateType.Update))
            {
                return -1;
            }

            if (provider.UpdateCustomer(customer))
            {
                OnDBChanged();
                OnWriteLog();
                return 1;
            }

            return 0;
        }

        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="customerId">客户ID</param>
        /// <returns>删除是否成功（true：成功，false：失败）</returns>
        public static bool DeleteCustomer(int customerId)
        {
            return provider.DeleteCustomer(customerId);
        }

        /// <summary>
        /// 判断客户名称是否存在
        /// </summary>
        /// <param name="customer">客户实体</param>
        /// <param name="type">判断的类型（添加或修改）</param>
        /// <returns>客户名称是否存在（true：已存在，false：未存在）</returns>
        public static bool CustomerExists(CustomerInfo customer, OperateType type)
        {
            return provider.CustomerExists(customer, type);
        }

        /// <summary>
        /// 得到客户信息
        /// </summary>
        /// <param name="customerId">客户ID</param>
        /// <returns>客户信息</returns>
        public static CustomerInfo GetCustomer(int customerId)
        {
            return provider.GetCustomer(customerId);
        }

        /// <summary>
        /// 得到客户列表
        /// </summary>
        /// <param name="pageId">当前页ID</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="condition">提取的条件</param>
        /// <returns>客户列表</returns>
        public static IList<CustomerInfo> GetCustomers(int pageId, int pageSize, string condition)
        {
            return provider.GetCustomers(pageId, pageSize, condition);
        }

        /// <summary>
        /// 得到客户列表
        /// </summary>
        /// <param name="condition">提取的条件</param>
        /// <returns>客户列表</returns>
        public static IList<CustomerInfo> GetCustomers(string condition)
        {
            return provider.GetCustomers(condition);
        }

        /// <summary>
        /// 得到客户列表
        /// </summary>
        /// <returns>客户列表</returns>
        public static IList<CustomerInfo> GetCustomers()
        {
            return provider.GetCustomers("");
        }

    }
}
